package cn.edu.swpu.cins.u_chain.frontend.controller.WorksController;

import cn.edu.swpu.cins.u_chain.frontend.entity.dto.ResultData;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.Works.WorksViewDetail;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.Works.WorksViewList;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.Works.WorksInfo;
import cn.edu.swpu.cins.u_chain.frontend.service.Works.WorksService;
import cn.edu.swpu.cins.u_chain.utils.EXCELUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("UChain/works")
public class WorksController {

    @Autowired
    private WorksService worksService;

    @PostMapping("addWorks")
    public ResultData addWorks(WorksInfo worksInfo) {
        worksService.addWorks(worksInfo);
        return new ResultData(true, "作品上传成功");
    }

    @PostMapping("uploadVideo")
    public ResultData uploadVideo(@RequestHeader("worksId") int worksId, @RequestPart("video") MultipartFile video) throws IOException {
        worksService.updateWorksVideo(worksId, video);
        return new ResultData(true, "上传成功");
    }

    @PostMapping("uploadPrintScreen")
    public ResultData insertPrintScreen(@RequestHeader("worksId") int worksId, @RequestPart("printScreens") MultipartFile[] printScreens) throws IOException {
        worksService.insertWorksPrintScreen(worksId, printScreens);
        return new ResultData(true, "上传截图成功");
    }

    @PostMapping("allWorks")
    public ResultData queryAllWorks() {
        WorksViewList viewList=worksService.selectAllWorks();
        return new ResultData(true, "所有的作品", viewList);
    }

    @RequestMapping("downExcel")
    public ResultData downExcel(@RequestBody List<Integer> worksIds, HttpServletResponse response) {
        worksService.downLoadExcel(worksIds,response);
        return new ResultData(true, "报表下载成功");
    }
}
